package com.nmdl.print.utils;

import static android.bluetooth.BluetoothClass.Device.Major.IMAGING;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BluetoothReceiver extends BroadcastReceiver {

    private static final String TAG = "hprt";
    private BluetoothListener listener;

    public BluetoothReceiver(BluetoothListener bluetoothListener) {
        this.listener = bluetoothListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
                listener.onDiscoveryStart();
                break;
            case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
                listener.onDiscoveryFinish();
                break;
            case BluetoothDevice.ACTION_FOUND:
                BluetoothDevice blueDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (blueDevice != null) {
                    Log.i(TAG, "onReceive: ${blueDevice.name}   ${blueDevice.bluetoothClass.majorDeviceClass}   ${blueDevice.address}");
                    if (blueDevice.getBluetoothClass().getMajorDeviceClass() == IMAGING) {
                        listener.onDeviceFound(blueDevice);
                    }
                }
                break;
            case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
                listener.onDeviceBondStateChange(intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1));
                break;
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                listener.onDeviceLost(device);
                break;
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                listener.onBluetoothStateChange(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
                break;
        }
    }

    public interface BluetoothListener {
        /**
         * 开始搜索蓝牙设备时调用
         */
        void onDiscoveryStart();

        /**
         * 当发现蓝牙设备时调用
         *
         * @param device 当前发现的蓝牙设备
         */
        void onDeviceFound(BluetoothDevice device);

        /**
         * 当前蓝牙设备配对状态改变时调用
         *
         * @param state 当前的配对状态
         */
        void onDeviceBondStateChange(Integer state);

        /**
         * 当设备断开时调用
         */
        void onDeviceLost(BluetoothDevice device);

        /**
         * 当蓝牙状态改变时调用
         *
         * @param state 当前蓝牙状态
         */
        void onBluetoothStateChange(Integer state);

        /**
         * 当搜索结束时调用
         */
        void onDiscoveryFinish();
    }
}
